package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpRequest;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：zhanghao
 * @name：XfService
 * @Date：2024/7/18 15:17
 */
@Service
public class XfService {
    @Autowired
    private ISysConfigService configService;

    public String sendxfai(String text) {

        String url = configService.selectConfigByKey("sys.xf.ai.URL");
        String apiKey = configService.selectConfigByKey("sys.xf.ai.APIKey");
        String apiSecret = configService.selectConfigByKey("sys.xf.ai.APISecret");
        String model = configService.selectConfigByKey("sys.xf.ai.model");

        JSONObject jsonObject = new JSONObject();
        jsonObject.set("model", model);
        JSONObject itemJson = new JSONObject();
        itemJson.set("role", "user");
        itemJson.set("content", text);
        List<JSONObject> list = new ArrayList<>();
        list.add(itemJson);
        jsonObject.set("messages", list);

        Map<String, String> heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("Authorization", "Bearer " + apiKey + ":" + apiSecret);

        System.out.println(url+","+jsonObject.toString());
        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toString()).timeout(60 * 1000).execute().body();
        System.out.println(result);
        return result;
    }
}
